<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            初始化dp数组:dp[i]表示以nums[i]为结尾的最大子数组和
            注意：因为是子数组和，所以只要加上去，就一定要么更大，要么更小，如果更小，直接切换

            dp[i]=Math.max(dp[i-1]+nums[i],nums[i])
            
            */
            var maxSubArray = function (nums) {
                let dp = new Array(nums.length).fill(0)
                dp[0] = nums[0]
                let result = nums[0]
                for (let i = 1; i < dp.length; i++) {
                    dp[i] = Math.max(dp[i - 1] + nums[i], nums[i])
                    result = Math.max(dp[i], result)
                }
                return result
            }
        </script>
    </body>
</html>
